package main
import "fmt"
import (
	r "github.com/solos/requests"
	"github.com/flipped-aurora/gin-vue-admin/server/api/v1"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/core"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/initialize"
	"go.uber.org/zap"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
)


//go:generate go env -w GO111MODULE=on
//go:generate go env -w GOPROXY=https://goproxy.cn,direct
//go:generate go mod tidy
//go:generate go mod download

// @title Swagger Example API
// @version 0.0.1
// @description This is a sample Server pets
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name x-token
// @BasePath /
func main() {
	global.GVA_VP = core.Viper() // 初始化Viper
	global.GVA_LOG = core.Zap()  // 初始化zap日志库
	zap.ReplaceGlobals(global.GVA_LOG)
	global.GVA_DB = initialize.Gorm() // gorm连接数据库
	initialize.Timer()
	initialize.DBList()
	if global.GVA_DB != nil {
		initialize.RegisterTables(global.GVA_DB) // 初始化表
		// 程序结束前关闭数据库链接
		db, _ := global.GVA_DB.DB()
		defer db.Close()
	}
	go func() {

		var busNovelsListService = service.ServiceGroupApp.AutoCodeServiceGroup.BusNovelsListService
		var busNovelsListApi = v1.ApiGroupApp.AutoCodeApiGroup.BusNovelsListApi

		var busNovelsService = service.ServiceGroupApp.AutoCodeServiceGroup.BusNovelsService
		var busNovelsApi = v1.ApiGroupApp.AutoCodeApiGroup.BusNovelsApi

		busNovelsListApi.DeleteAllBusNovelsList()
		busNovelsApi.DeleteAllBusNovels()

		kwargs := r.M{}
		options := r.M{
			"timeout": 10,
		}
		cookies := map[string]string{
			"user": "solos",
		}
		headers := map[string]string{
			"content-Type": "application/json",
		}
	
		data := map[string]string{
			"hello": "world",
		}

	
		req := &r.Request{Args: kwargs}
		resp, _ := req.MakeRequest("GET", "http://api.zhuishushenqi.com/ranking/gender", r.Timeout(10), r.Headers(headers), r.Cookies(cookies), r.Options(options), r.Data(data))
	

		// 

		result :=utils.JSONToMap(resp.Content)

		for _, item := range result["female"].([]interface{}) {

			fmt.Println(item)
			md , _ := item.(map[string]interface{})

			// var name = md["title"]
			// var uuid = md["_id"]

			name := md["title"].(string)

			uuid := md["_id"].(string)

			busNovelsList :=busNovelsListApi.CreateBusNovelsListByObject( name , uuid)
			if err := busNovelsListService.CreateBusNovelsList(busNovelsList); err != nil {
				global.GVA_LOG.Error("创建失败!", zap.Error(err))
			}else{
				// getNovels(uuid)

				resp, _ := req.MakeRequest("GET", "http://api.zhuishushenqi.com/ranking/" + uuid, r.Timeout(10), r.Headers(headers), r.Cookies(cookies), r.Options(options), r.Data(data))

				result :=utils.JSONToMap(resp.Content)
				ranking := result["ranking"].(interface{})



				for _, item := range ranking.(map[string]interface{})["books"].([]interface{}) {
			
					md , _ := item.(map[string]interface{})
	

					uuid := md["_id"].(string)
					name := md["title"].(string)

					author := md["author"].(string)

					cover := md["cover"].(string)

					category := md["minorCate"].(string)

					shortInfo := md["shortIntro"].(string)

					retent := md["retentionRatio"].(string)

					latelyFollower := md["latelyFollower"].(float64)
	// 				uuid string , name string , author string , cover string , category string ,
	// shortInfo string , retent string , latelyFollower *int 

					busNovels :=busNovelsApi.CreateBusNovelsByObject( uuid , name , author ,cover , category , shortInfo , retent , &latelyFollower)
					if err := busNovelsService.CreateBusNovels(busNovels); err != nil {
						global.GVA_LOG.Error("创建失败!", zap.Error(err))
					}
					fmt.Println(name)
			
				}
			}

		}
		// fmt.Println(resp.Content)
	}()
	core.RunWindowsServer()
}
